package sms

import (
	"context"
	"fmt"
	"github.com/go-resty/resty/v2"
	"time"
)

type SendCodeReq struct {
	Phone       string `json:"phone"`        // 手机号
	AppId       string `json:"app_id"`       // 分配的appId
	MessageType string `json:"message_type"` // 验证类型
	// Code        string `json:"code"`         // 验证码
	RequestId   string `json:"request_id"`   // 业务幂等Id
	SignName    string `json:"sign_name"`
}

type SendCodeRes struct {
	Message string `json:"message"`
}

func (s *SmsClient) SendCode(ctx context.Context, req *SendCodeReq) (res *SendCodeRes, err error) {
	url := fmt.Sprintf("%s/api/v1/code/send", s.endpoint)
	result := &smsInvokeRes[SendCodeRes]{}
	errRes := &smsErrorRes{}
	client := resty.New()
	_, err = client.SetTimeout(1 * time.Minute).R().
		SetContext(ctx).
		SetHeader("Content-Type", "application/json").
		SetBody(req).
		SetResult(result).
		SetError(errRes).
		Post(url)
	if err != nil {
		return
	}
	if !result.Success {
		err = fmt.Errorf("SendCode Err:%s", errRes.Msg)
		return
	}
	res = result.Data
	return
}
